//
//  HMNavigationController.swift
//  Weibo
//
//  Created by Apple on 2017/7/10.
//  Copyright © 2017年 KY. All rights reserved.
//

import UIKit
//  自定义导航控制器，监听push操作
class HMNavigationController: UINavigationController, UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()

        //  设置边缘手势代理
        self.interactivePopGestureRecognizer?.delegate = self
    }

    //  重写push方法，监听其操作
    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        if viewControllers.count > 0 {
            //  隐藏tabbar, 会出现黑影，解决办法设置window的背景色，设置tabbar的背景图片
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: animated)
        if viewControllers.count > 1 {
            viewController.navigationItem.title = "当前是第\(viewControllers.count)级控制器"
            var title: String = ""
            if viewControllers.count == 2 {
                //  获取上一级控制的标题
                title = viewControllers.first!.title!
            } else {
                //  其它设置返回文字
                title = "返回"
            }
            //  设置导航栏按钮
            viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: title, imageName: "navigationbar_back_withtext", target: self, action: #selector(backAction))
        }
        
    }
    
    //  MARK:   --返回
    @objc func backAction() {
        popViewController(animated: true)
    }
    
    //  MARK: -- UIGestureRecognizerDelegate
    func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
        if viewControllers.count == 1 {
            //  跟视图控制器不开启收拾
            return false
        }
        return true
    }
}
